package app.foodnote;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SinglePlaceActivity extends Activity {

	// Alert Dialog Manager
	AlertDialogManager alert = new AlertDialogManager();
	
	// Google Places
	GooglePlaces googlePlaces;
	
	// Place Details
	PlaceDetails placeDetails;
	
	// Progress dialog
	ProgressDialog pDialog;
	
	// KEY Strings
	public static String KEY_REFERENCE = "reference";
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.single_place);
		
		Intent i = getIntent();
		
		// Place reference id
		String reference = i.getStringExtra(KEY_REFERENCE);
		
		new LoadSinglePlaceDetails().execute(reference);
	}
	
	class  LoadSinglePlaceDetails extends AsyncTask<String, String, String> {
		
		@Override
		protected void onPreExecute(){
			super.onPreExecute();
			pDialog = new ProgressDialog(SinglePlaceActivity.this);
			pDialog.setMessage("Loading ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			String reference = params[0];
			googlePlaces = new GooglePlaces();
			
			try {
				placeDetails = googlePlaces.getPlaceDetails(reference);
			} catch (Exception e){
				e.printStackTrace();
			}
			return null;
		}
		protected void onPostExecute(String file_url){
			pDialog.dismiss();
			runOnUiThread(new Runnable() {
				
				public void run() {
					// Updating parsed Places into ListView
					if(placeDetails != null){
						String status = placeDetails.status;
						
						if(status.equals("OK")){
							if (placeDetails.result != null) {
								String name = placeDetails.result.name;
								String address = placeDetails.result.formatted_address;
								String phone = placeDetails.result.formatted_phone_number;
	                            String latitude = Double.toString(placeDetails.result.geometry.location.lat);
	                            String longitude = Double.toString(placeDetails.result.geometry.location.lng);
	
	                            Log.d("Place ", name + ", " + address + ", " + phone + ", " + 
	                            		latitude + ", " + longitude);
	                            // Displaying all the details in the view
	                            // single_place.xml
	                            TextView lbl_name = (TextView) findViewById(R.id.txt_name);
	                            TextView lbl_address = (TextView) findViewById(R.id.txt_address);
	                            TextView lbl_phone = (TextView) findViewById(R.id.txt_phone);
	                            TextView lbl_location = (TextView) findViewById(R.id.txt_location);
	
	                            // Check for null data from google
	                            // Sometimes place details might missing
	                            name = name == null ? "Không tìm thấy" : name;
	                            address = address == null ? "Không tìm thấy" : address;
	                            phone = phone == null ? "Không tìm thấy" : phone;
	                            latitude = latitude == null ? "Không tìm thấy" : latitude;
	                            longitude = longitude == null ? "Không tìm thấy" : longitude;
	                            
	                            lbl_name.setText(name);
	                            lbl_address.setText(address);
	                            lbl_phone.setText(phone);
	                            lbl_location.setText(latitude + ", " + longitude);
							}
					} else if(status.equals("ZERO_RESULTS")){
                        alert.showAlertDialog(SinglePlaceActivity.this, "Địa Điểm Xung Quanh",
                                "Không có nơi nào được tìm thấy.",
                                false);
                    }
                    else if(status.equals("UNKNOWN_ERROR"))
                    {
                        alert.showAlertDialog(SinglePlaceActivity.this, "Lỗi",
                                "Không biết lỗi xảy ra.",
                                false);
                    }
                    else if(status.equals("OVER_QUERY_LIMIT"))
                    {
                        alert.showAlertDialog(SinglePlaceActivity.this, "Lỗi",
                                "Truy vấn tới Google Place bị giới hạn.",
                                false);
                    }
                    else if(status.equals("REQUEST_DENIED"))
                    {
                        alert.showAlertDialog(SinglePlaceActivity.this, "Lỗi",
                                "Yêu cầu bị từ chối.",
                                false);
                    }
                    else if(status.equals("INVALID_REQUEST"))
                    {
                        alert.showAlertDialog(SinglePlaceActivity.this, "Lỗi",
                                "Yêu cầu không hợp lệ.",
                                false);
                    }
                    else
                    {
                        alert.showAlertDialog(SinglePlaceActivity.this, "Lỗi",
                                "Xin lỗi đã xảy ra lỗi.",
                                false);
                    }
				}
				else{
                alert.showAlertDialog(SinglePlaceActivity.this, "Lỗi",
                        "Xin lỗi đã xảy ra lỗi.",
                        false);
				}
			}});
		}
	}
}
